package com.example.lijianwen.recyclerviewtest_401ljw.model;

/**
 * Created by lijianwen on 2017/4/25.
 */
import com.example.lijianwen.recyclerviewtest_401ljw.bean.TcareBean;
import com.example.lijianwen.recyclerviewtest_401ljw.iface.TcareListener;
import com.example.lijianwen.recyclerviewtest_401ljw.iface.Tcareiface;
import com.example.lijianwen.recyclerviewtest_401ljw.service.TcareService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/4/20.
 */
//model 获取文章数据
public class TcareModel implements Tcareiface{
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public TcareModel()
    {//使用retrofit--1
        retrofit=new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    //获取文章数据
    @Override
    public void getResultList(String tcase, int page, String sessionID, final TcareListener tcareListener) {
        //使用retrofit--2
        TcareService tcareService=retrofit.create(TcareService.class);
        Call<List<TcareBean>> call=tcareService.getResult(tcase,page,sessionID);
        //使用retrofit--3
        call.enqueue(new Callback<List<TcareBean>>() {
            @Override
            public void onResponse(Call<List<TcareBean>> call, Response<List<TcareBean>> response) {
                if(response.isSuccessful()&&response!=null)
                {
                    tcareListener.onResponse(response.body());
                }
                else
                    tcareListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<TcareBean>> call, Throwable t) {
                tcareListener.onFail(t.toString());
            }
        });
    }
}